package com.mj.listen2._01_排序.cmp;

/**
 * 冒泡排序优化1，数组有序的话，优化效果会比较好
 */
public class _01_BubbleSort2<T extends Comparable<T>> extends Sort<T> {
    @Override
    protected void sort() {
        for (int end = array.length - 1; end > 0; end--) {
            // 是否是有序的
            boolean isSorted = true;
            for (int i = 1; i <= end; i++) {
                if (cmp(i - 1, i) > 0) {
                    swap(i - 1, i);
                    isSorted = false;
                }
            }
            if (isSorted) break;
        }
    }
}
